#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
using namespace std;
vector<int> arr;
int Partition1(vector<int> &arr, int l, int r)
{
    int pivot = arr[r];
    int i = l, j = r;
    do
    {
        while (i <= j && arr[i] < pivot)
            i++;
        while (i <= j && pivot < arr[j])
            j--;
        if (i <= j)
        {
            swap(arr[i], arr[j]);
            i++, j--;
        }
    } while (i <= j);
    return j;
}
int Partition2(vector<int> &arr, int l, int r)
{
    int pivot = arr[r];
    int i = l - 1;
    for (int j = l; j < r; ++j)
    {
        if (arr[j] < pivot)
        {
            i++;
            swap(arr[i], arr[j]);
        }
    }
    swap(arr[i + 1], arr[r]);
    return i + 1;
}
void quickSort(vector<int> &arr, int l, int r)
{
    if (l < r)
    {
        int q = Partition1(arr, l, r);
        quickSort(arr, l, q);
        quickSort(arr, q + 1, r);
    }
}
void show(vector<int> &arr)
{
    for (auto &it : arr)
    {
        printf(" %d", it);
    }
    printf("\n");
}
int main()
{
    arr = {101, 100, 98, 4, 2, 1, -1};
    printf("源数组:\n");
    show(arr);
    printf("Partition1:\n");
    int j = Partition1(arr, 0, arr.size() - 1);
    show(arr);
    printf("pivot:%d\nPartition2:\n", j);
    arr = {101, 100, 98, 4, 2, 1, -1};
    j = Partition2(arr, 0, arr.size() - 1);
    show(arr);
    printf("pivot:%d\nSorted:", j);
    quickSort(arr, 0, arr.size() - 1);
    show(arr);
    return 0;
};